/*=======================敌人=========================*/
function Enemy(type){
	this.oScore = document.getElementsByClassName("score")[0];
	this.init = function(){
		this.ele = document.createElement("div");
		document.body.appendChild(this.ele);
		this.uniqueNum = Math.round(Math.random()*10000000000) + new Date().getTime().toString();
		GameEngine.enemies[this.uniqueNum] = this;
		switch (type){
			case GameEngine.ENEMY_TYPE_SMALL:
				this.ele.className = "enemy-small";
				this.ele.hp = 1;
				this.speed = 10;
				this.score = 100;
				break;
			case GameEngine.ENEMY_TYPE_MIDDLE:
				this.ele.className = "enemy-middle";
				this.ele.hp = 5;
				this.speed = 5;
				this.score = 200;
				break;
			case GameEngine.ENEMY_TYPE_LARGE:
				this.ele.className = "enemy-large";
				this.ele.hp = 10;
				this.speed = 2;
				this.score = 500;
				break;
			default:
				break;
		}
		//敌人出现的位置
		var minVal = GameEngine.bg.left();
		var maxVal = GameEngine.bg.left() + GameEngine.bg.width() - this.ele.offsetWidth;
		var randomPos = Math.round(Math.random()*(maxVal - minVal)) +minVal;
		this.ele.style.left = randomPos + "px";
		this.ele.style.top = -this.ele.offsetHeight + "px";
		
		return this;
	}
	this.move = function(){
		this.t = setInterval(function(){
			this.ele.style.top = this.ele.offsetTop + this.speed + "px";
			//判断敌军是否出界
			if(this.ele.offsetTop >= GameEngine.bg.height()){
				clearInterval(this.t);
				this.destroy();
			}
		}.bind(this),30);
	}
	this.hurt = function(){
		this.ele.hp--;
		if(this.ele.hp == 0){
			this.explode();
		}
	}
	this.destroy = function(){
		this.oScore.innerHTML = "分数：" + GameEngine.totalscore;
		delete GameEngine.enemies[this.uniqueNum];
		this.ele.remove();
	}
	this.explode = function(){
		clearInterval(this.t);
		this.destroy();
	}
	this.left = function(){
		return this.ele.offsetLeft;
	}
	this.width = function(){
		return this.ele.offsetWidth;
	}
	this.top = function(){
		return this.ele.offsetTop;
	}
	this.height = function(){
		return this.ele.offsetHeight;
	}
}